package com.xn.app.muying.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xn.app.muying.R;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.view.ChartView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体温监测 查看曲线
 * Created by gaoge on 2017/2/16.
 */
public class TemperatureSeeCurveActivity extends BaseActivity implements View.OnClickListener {
    //返回键
    private ImageView img_back = null;
    //标题
    private TextView title_name = null;
    //曲线图
    private ChartView chartview = null;
    //温度范围字段
    private TextView temperature_text = null;
    //温度范围显示
    private TextView temperature_number = null;
    //开始监测时间
    private TextView start_time = null;
    //持续时间
    private TextView continuous_time = null;
    //备注布局
    private LinearLayout note_linearlayout = null;
    //处理措施布局
    private LinearLayout measures_linearlayout = null;
    //图片记录布局
    private LinearLayout img_linearlayout = null;
    //x轴坐标对应的数据
    private List<String> xValue = new ArrayList<>();
    //y轴坐标对应的数据
    private List<Integer> yValue = new ArrayList<>();
    //折线对应的数据
    private Map<String, Float> value = new HashMap<>();
    //获取数据动作
    private Intent intent = null;
    //记录当前监测状态  true 表示正在监测   false 表示监测完成
    private boolean state = false;
    //记录当前看护类别  0发烧看护  1蹬被子看护  2洗澡看护
    private int careType = 0;
    //记录开始监测时间
    private String startTime = null;
    //记录监测时长
    private String continuousTime = null;
    //记录当前测量到的时间
    private String timeData = null;
    private String[] timeDatas = null;
    //记录当前测量到的时间
    private String temperatureData = null;
    private String[] temperatureDatas = null;
    //获取要显示的温度值
    float f = 0.0f;
    float l = 0.0f;
    //判断上级页面
    private int type = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.temperature_see_curve_activity);
        //初始化
        init();
    }

    //初始化组件功能实现
    private void init() {
        img_back = (ImageView) findViewById(R.id.img_back);
        title_name = (TextView) findViewById(R.id.title_name);
        chartview = (ChartView) findViewById(R.id.chartview);
        temperature_text = (TextView) findViewById(R.id.temperature_text);
        temperature_number = (TextView) findViewById(R.id.temperature_number);
        start_time = (TextView) findViewById(R.id.start_time);
        continuous_time = (TextView) findViewById(R.id.continuous_time);
        note_linearlayout = (LinearLayout) findViewById(R.id.note_linearlayout);
        measures_linearlayout = (LinearLayout) findViewById(R.id.measures_linearlayout);
        img_linearlayout = (LinearLayout) findViewById(R.id.img_linearlayout);
        //获取数据
        intent = getIntent();
        //正在监测的数据获取
        type = intent.getIntExtra("type", 0);
        state = intent.getBooleanExtra("state", false);
        careType = intent.getIntExtra("careType", 0);
        startTime = intent.getStringExtra("startTime");
        continuousTime = intent.getStringExtra("continuousTime");
        temperatureData = intent.getStringExtra("temperatureData");
        timeData = intent.getStringExtra("timeData");
        //设置标题
        title_name.setText(intent.getStringExtra("time"));
        //并处理数据
        //添加X轴数据
        if (timeData != null && !timeData.equals("")) {
            timeDatas = timeData.split(";");
            for (int i = 0; i < timeDatas.length; i++) {
                xValue.add(timeDatas[i]);
            }
        }
        //添加折线图数据
        f = 0.0f;
        l = 0.0f;
        if (temperatureData != null && !temperatureData.equals("")) {
            temperatureDatas = temperatureData.split(";");
            for (int i = 0; i < temperatureDatas.length; i++) {
                value.put(xValue.get(i), Float.valueOf(temperatureDatas[i]));
                //查找最小的
                if (careType == 0) {
                    if (f < Float.valueOf(temperatureDatas[i])) {
                        f = Float.valueOf(temperatureDatas[i]);
                    }
                } else if (careType == 1) {
                    if (f == 0.0f) {
                        f = Float.valueOf(temperatureDatas[i]);
                    }
                    if (f > Float.valueOf(temperatureDatas[i])) {
                        f = Float.valueOf(temperatureDatas[i]);
                    }
                } else {
                    if (f < Float.valueOf(temperatureDatas[i])) {
                        f = Float.valueOf(temperatureDatas[i]);
                    }

                    if (l == 0.0f) {
                        l = Float.valueOf(temperatureDatas[i]);
                    }
                    if (l > Float.valueOf(temperatureDatas[i])) {
                        l = Float.valueOf(temperatureDatas[i]);
                    }
                }

            }
        }

        //按照类别显示不同数据
        showData(state, careType);

        //显示数据
        if (value.size() < 8) {
            int number = 8 - value.size();
            for (int i = 0; i < number; i++) {
                xValue.add((i + 1) + ":10");
                value.put((i + 1) + ":10", 0.0f);
            }
            chartview.setSelectIndex(8 - number);
        } else {
            chartview.setSelectIndex(value.size());
        }
        for (int i = 4; i <= 10; i++) {
            yValue.add(5 * i);
        }

        chartview.setValue(value, xValue, yValue);
        img_back.setOnClickListener(this);
    }

    /**
     * 显示数据列表
     *
     * @param state    监测状态  true 正在监测
     * @param careType 看护类别
     */
    private void showData(boolean state, int careType) {
        switch (careType) {
            //发烧看护
            case 0:
                temperature_text.setText("最高温度");
//                if (!state) {
//                    note_linearlayout.setVisibility(LinearLayout.VISIBLE);
//                    measures_linearlayout.setVisibility(LinearLayout.VISIBLE);
//                    img_linearlayout.setVisibility(LinearLayout.VISIBLE);
//                }
                break;
            //蹬被子看护
            case 1:
                temperature_text.setText("最低温度");
                break;
            //洗澡看护
            case 2:
                temperature_text.setText("温度范围");
                temperature_number.setTextColor(0xffADCC00);
                break;
        }
        if (type == 0) {
            if(careType!=2){
                temperature_number.setText(f + "℃");
            }else{
                temperature_number.setText(l+"~"+f + "℃");
            }

        } else {
            temperature_number.setText(intent.getStringExtra("data"));
        }

        start_time.setText(startTime);
        continuous_time.setText(continuousTime);
    }

    //单击事件
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回键
            case R.id.img_back:
                if (type == 0) {
                    Intent intent = new Intent(TemperatureSeeCurveActivity.this, TemperatureMonitoringActivity.class);
                    startActivity(intent);
                }
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (type == 0) {
                Intent intent = new Intent(TemperatureSeeCurveActivity.this, TemperatureMonitoringActivity.class);
                startActivity(intent);
            }
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
